package cn.jietuo.first.class07;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/7/19
 * @description: 递归遍历二叉树
 */
public class Code01_RecursiveTraversalBT {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int value) {
            this.value = value;
        }
    }

    /**
     * 先序遍历
     */
    public static void prePrint(Node head) {
        if (head == null) {
            return;
        }
        System.out.println(head.value);
        prePrint(head.left);
        prePrint(head.right);
    }

    /**
     * 中序遍历
     *
     * @param head
     */
    public static void midPrint(Node head) {
        if (head == null) {
            return;
        }
        midPrint(head.left);
        System.out.println(head.value);
        midPrint(head.right);
    }

    /**
     * 后序遍历
     *
     * @param head
     */
    public static void lastPrint(Node head) {
        if (head == null) {
            return;
        }
        lastPrint(head.left);
        lastPrint(head.right);
        System.out.println(head.value);
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);

        prePrint(head);
        System.out.println("========");
        midPrint(head);
        System.out.println("========");
        lastPrint(head);
        System.out.println("========");

    }

}
